<?php
/**
 * Created by PhpStorm.
 * User: zhang_000
 * Date: 2015/9/10
 * Time: 15:08
 */

use Phalcon\Mvc\User\Component;

class Menu extends Component{

    /**
     * 定义网站菜单
     * @var array
     */
    private $Menus = array(

        "account" =>  array(
            "label"   => "图书管理系统",
            "control" =>"manager",
            "action"  => "index",
            "url"     => "#/home",
            "subMenu" => array(
                "jibenxinxi" => array(
                    "label"   => "用户管理",
                    "control" =>"manager",
                    "action"  => "user",
                    "icon"    => "icon-credit-card",
                    'url'     => "#/userlist"
                ),
                "gongxixinxi" => array(
                    "label"   => "图书管理",
                    "control" =>"manager",
                    "action"  => "gongsixinxi",
                    "icon"    => " icon-envelope-alt",
                    'url'     => "#/draggable"
                ),
                "gongsixiangce" => array(
                    "label"   => "借还管理",
                    "control" =>"manager",
                    "action"  => "gongsixiangce",
                    "icon"    => " icon-picture",
                    'url'     => "/account/gongsixiangce"
                )
            )
        ),
        "img" =>  array(
            "label"   => "图片管理",
            "control" =>"manager",
            "action"  => "index",
            "url"     => "#/spider",
            "subMenu" => array(
                "jibenxinxi" => array(
                    "label"   => "抓去图片",
                    "control" =>"manager",
                    "action"  => "user",
                    "icon"    => "icon-credit-card",
                    'url'     => "#/spider"
                ),

            )
        )
    );


    /**
     * 获取父级的菜单
     * @return string
     */
    public function getParentMenu(){
        $_currCtrl =  $this->dispatcher->getControllerName();
        $_currAction = $this->dispatcher->getActionName();
        $html = "";
        foreach($this->Menus as $arr){
            if($_currCtrl == $arr['control']){
                $html .='<li class="cursor active"><a href="'.$arr['url'].'">'.$arr['label'].'</a></li>';
            }else{
                $html .='<li class="cursor"><a href="'.$arr['url'].'">'.$arr['label'].'</a></li>';
            }
        }
        return $html;
    }

    /**
     * 获取所有的菜单，并拼成html
     */
    public function getMenus(){

        $htmls = "<ul id='menuBar'>";
        foreach($this->Menus as $arr){
            $htmls .="<li><a href='".$arr["url"]."'>".$arr["label"]."</a>";
            if($arr["subMenu"] != null){
                $subHtmls = "<ul>";
                foreach($arr["subMenu"] as $subArr){
                    $subHtmls .="<li><a href='".$subArr["url"]."'>".$subArr["label"]."</a></li>";
                }
                $subHtmls .="</ul>";
                $htmls .= $subHtmls;
            }
            $htmls .="</li>";
        }
        $htmls .= "</ul>";
        return $htmls;
    }



    /**
     * 获取子集的菜单
     * @return string
     */
    public function getSubMenu(){
        $_currCtrl =  $this->dispatcher->getControllerName();
        $_currAction = $this->dispatcher->getActionName();
        if(isset($this->Menus[$_currCtrl]["subMenu"])){
            $subArr = $this->Menus[$_currCtrl]["subMenu"];
            $html = "";
            foreach($subArr as $arr){
                if($_currAction == $arr["action"]){
                    $html .='<li class="cursor menuActive"><a href="'.$arr['url'].'"><i class="'.$arr["icon"].'"></i>'.$arr['label'].'</a></li>';
                }else{
                    $html .='<li class="cursor"><a href="'.$arr['url'].'"><i class="'.$arr["icon"].'"></i>'.$arr['label'].'</a></li>';
                }
            }

            return $html;
        }
    }
}